By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 Homemade NAS.md

View raw Download
text/x-script.python • 9.73 kiB
Python script, Unicode text, UTF-8 text executable

title: Cheap, homemade NAS with Raspberry Pi topics: ["hardware", "server", "networking", "raspberry pi", "nas", "gnu/linux"] DATE: 2024-05-21 ---

This is a very simple, cheap and quick way to get networked storage at home. It should not cost more than €120 for all the components (assuming you've got a network you can plug it into). It also offers more flexibility than a commercial NAS, because you can install any software you want on it. And if you already use the Raspberry Pi for something else, you can just add this to it and not worry about an extra device you need power, networking, space and maintenance for.

A Raspberry Pi is already 4-bay since it has 4 USB ports. You can use a hub for more drives, since no HDD will use the full potential of USB 3.0. Just keep in mind you need an adapter for each.

However, for large-scale use, this method should be done with a different type of computer, not a Raspberry Pi.

I assume you know some things about GNU/Linux, SSH, and networking. This guide is not for that; if you don't know these things read some material when you need it.

If you think this is for you, keep reading.

Materials

  • Raspberry Pi, or another small computer that can run GNU/Linux, has a network and USB. I'm using the Pi 4 with 8GB of RAM, but you can use any model.

  • microSD card of at least 16GB (this won't be your primary storage but a boot device)

  • SATA drive(s), or USB drives - SATA drives are cheaper. Format the drive on your own computer; it's easier that way. Choose ext4 as the filesystem.

  • Suitable adapter(s) for the SATA drive(s).

    • For Raspberry Pi 5 you could get a SATA HAT as it has PCIe, which is more efficient.

    • If your other kind of small computer has SATA, you can use that.

    • Otherwise get a USB to SATA adapter, one per drive. For older Raspberry Pis, don't get a SATA HAT, as they use USB as well but cost more for some reason. Please make sure to get one that can take power from a separate source if you're going to use 3.5" drives.

  • A power supply for your computer. For Raspberry Pi, a phone charger of 5V and 3A (marketed as fast charging) is enough.

  • If you're using 3.5" SATA drives, a power supply for the adapter(s) if they don't come with one.

    1. 5" drives need more power than USB can provide.

  • If you want to use wired networking, a cable. If you want to use wireless networking and your computer doesn't have Wi-Fi, some Wi-Fi adapter.

  • If you have an SBC you should have a case, but it's not required. You can also use lego.

  • Some other computer. An Android phone is fine; just install Termux.

My setup

  • Raspberry Pi 4 with 8GB of RAM (€80)

  • No-name 3.5" SATA to USB adapter with power supply (€18)

  • One Toshiba P300 4TB 3.5" SATA drive (€90). No RAID, but I copy important files to my own PC.

  • Raspberry Pi 15W power adapter (€9)

  • A 1.5m Cat6 cable (€3)

  • SanDisk Extreme 128GB microSD card (€15)

  • Official case (€6). Don't judge me, it's just a piece of plastic, and it's cheap. Don't forget Raspberry Pi is not Apple.

Total: €221. Now, this is not the cheapest setup. I use that Pi for other things as well. You can certainly have a cheaper setup:

Low-cost setup

  • Raspberry Pi 4 with 1GB of RAM (€40). I'd insist on getting a big Pi, as the Zero doesn't have wired networking, only one USB port, and too little RAM.

  • Cheaper no-name 3.5" SATA to USB adapter with power supply (€10)

  • One Toshiba P300 1TB 3.5" SATA drive (€50)

  • Phone charger (€6, if not already owned)

  • No cable, use Wi-Fi (I assume you have Wi-Fi)

  • SanDisk Ultra 32GB microSD card (€7)

  • No case

Total: €113, with drive included!

What about a "real" NAS?

Cheapest NAS I could find is €180! It is 1-bay and has 1GB of RAM. It doesn't do anything else, it doesn't use standard protocols, it runs a proprietary OS, it's bulkier and much more expensive. (The brand is Synology.)

No, it doesn't come with drives. Using the same drive as above, it would reach €230.

Well, it's plug-and-play but that makes it much more restricted. Also, it does have a warranty and professional support. However, it's not the kind of NAS a business would use either.

Setup (skip if you already have the computer set up)

  1. Flash your microSD card. Raspberry Pi recommends their own (free) software, Choose an OS without a GUI: Raspberry Pi OS Lite, Ubuntu enable SSH and preset the Wi-Fi settings if you're going

  2. Put the card in the computer, plug in the network and start it.

  3. Find out the IP address of the computer. You can use your router's web interface, or a network Now is a good time to set up a static IP address for the computer, forward its ports dynamic DNS if you want to access it from the internet. YDNS is a imple and free service for that. Their official client is also free software.

  4. Connect to your server via SSH. On GNU/Linux or Macintosh you can use the preinstalled ssh On Windows, you can use PuTTY.

  5. Set a root password with sudo passwd. Then su.

  6. Update the system with apt update && apt -y upgrade.

Set up the drives

You must edit the /etc/fstab to automatically mount the drives on boot. If you know how to do this, you can do it yourself. For the rest of the tutorial the mount point will be /storage.

  1. Turn off the computer and plug in the drive(s).

  2. Turn on the computer.

  3. lsblk to find the drive(s). They should be /dev/sd followed by a letter. For now we'll

  4. Find the drive's UUID with blkid /dev/sdX1.

  5. Add the drive to the /etc/fstab as such:

    =00000000-0000-0000-0000-000000000000 /storage ext4 defaults 0 2

Replace the UUID with the one you found and, if you want, the mount point (/storage in this case) with another one, and the filesystem to match the one you formatted the drive with. Change the last column to 0 to disable fsck for that drive.

  1. mkdir /storage to create the mount point.

  2. Reboot.

Give each user their private directory

We'll mirror the home layout. However, we won't move the existing home directories, because then users can't use the SD card, and you may have some other apps to run on the server that you want to be separate from the mass storage.

Write a systemd service to create the directories on boot. Give it a name inside /etc/systemd/system/, like /etc/systemd/system/setup_storage_dirs.service

[Unit]
Description=Update private user storage spaces

[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /usr/local/bin/setup_storage_dirs.py

[Install]
WantedBy=multi-user.target

And the script /usr/local/bin/setup_storage_dirs.py:

#!/bin/python3

import os
import subprocess
from pathlib import Path

homes = Path("/home").glob("*")
storage_path = Path("/storage")   # change this if you changed the mount point

for home in homes:
   if home.is_dir():
       user = home.name
       storage = storage_path/user
       bound = home/"Storage"    # change this if you'd like a different name
       if not storage.exists():
           os.makedirs(storage)
           os.chown(storage, home.stat().st_uid, home.stat().st_gid)
           os.chmod(storage, 0o700)
       if not bound.is_dir():
           os.makedirs(bound)
           os.chown(bound, home.stat().st_uid, home.stat().st_gid)
           os.chmod(bound, 0o700)
       
       # Using bind mounts; some file managers don't like symlinks
       subprocess.run(["mount", "--bind", str(storage), str(bound)])

Reboot. Now each user has a private directory on your drive at ~/Storage. You can add more users; each will get their own directory only they can access.

Using the server

This guide does not cover setting up sharing protocols. However, we will use SFTP (FTP over SSH) because it's plug-and-play, the speed difference is negligible, it integrates with system users, it's native (works in the system file manager, no web needed) and, last but not least, it's encrypted.

Since you set up the OS to use SSH, you can use SFTP. All file managers can connect to SFTP servers, except for Windows Explorer, in which case you can use WinSCP. This is not my problem, it's Microsoft's problem. Even the preinstalled file manager on Samsung phones can use an SFTP add-on. Not like Next"cloud"¹ is more integrated.

If you want to mount it there is the sshfs program as well. This is useful if you don't like GUI file managers or want easier access to the files.

SFTP in the terminal

sftp -oPort=22 user@server

If you use port 22, you can omit the oPort option. The commands are identical to the ones in the classic ftp client.

SSHFS

sshfs user@server:/storage/user ~/server

Second argument is the "mount point". If you want to unmount it:

fusermount -u ~/server

SFTP in the file manager

Graphically, you can access SFTP by typing sftp://user@server in the address bar (works in most cases) or by finding an option to connect to a server.

Nemo (GNU/Linux)

File > Connect to server > select type SSH

Material Files (Android)

Menu > Add storage > SFTP server

Amaze (Android)

Plus button > Cloud connection > SCP/SFTP Connection

Windows

Windows Explorer doesn't natively support SFTP. WinSCP is a good client for it, and it's free software.

Samsung phone file manager (Android)

Storage space section > Network storage space > Update the app when prompted > Plus > SFTP server

                
                    
1
---
2
title: Cheap, homemade NAS with Raspberry Pi
3
topics: ["hardware", "server", "networking", "raspberry pi", "nas", "gnu/linux"]
4
DATE: 2024-05-21
5
---
6
7
This is a very simple, cheap and quick way to get networked storage at home. It should not cost
8
more than €120 for all the components (assuming you've got a network you can plug it into). It also
9
offers more flexibility than a commercial NAS, because you can install any software you want on it.
10
And if you already use the Raspberry Pi for something else, you can just add this to it and not
11
worry about an extra device you need power, networking, space and maintenance for.
12
13
A Raspberry Pi is already 4-bay since it has 4 USB ports. You can use a hub for more drives,
14
since no HDD will use the full potential of USB 3.0. Just keep in mind you need an adapter for each.
15
16
However, for large-scale use, this method should be done with a different type of computer, not
17
a Raspberry Pi.
18
19
I assume you know some things about GNU/Linux, SSH, and networking. This guide is not for that;
20
if you don't know these things read some material when you need it.
21
22
If you think this is for you, keep reading.
23
24
Materials
25
---------
26
27
* Raspberry Pi, or another small computer that can run GNU/Linux, has a network and USB. I'm using
28
the Pi 4 with 8GB of RAM, but you can use any model.
29
* microSD card of at least 16GB (this won't be your primary storage but a boot device)
30
* SATA drive(s), or USB drives - SATA drives are cheaper. Format the drive on your own computer;
31
it's easier that way. Choose ext4 as the filesystem.
32
* Suitable adapter(s) for the SATA drive(s).
33
* For Raspberry Pi 5 you could get a SATA HAT as it has PCIe, which is more efficient.
34
* If your other kind of small computer has SATA, you can use that.
35
* Otherwise get a USB to SATA adapter, one per drive. For older Raspberry Pis, don't get a
36
SATA HAT, as they use USB as well but cost more for some reason. Please make sure to get one
37
that can take power from a separate source if you're going to use 3.5" drives.
38
* A power supply for your computer. For Raspberry Pi, a phone charger of 5V and 3A (marketed
39
as fast charging) is enough.
40
* If you're using 3.5" SATA drives, a power supply for the adapter(s) if they don't come with one.
41
3.5" drives need more power than USB can provide.
42
* If you want to use wired networking, a cable. If you want to use wireless networking and your
43
computer doesn't have Wi-Fi, some Wi-Fi adapter.
44
* If you have an SBC you should have a case, but it's not required. You can also use lego.
45
* Some other computer. An Android phone is fine; just install [Termux](https://termux.com/).
46
47
### My setup
48
49
* Raspberry Pi 4 with 8GB of RAM (€80)
50
* No-name 3.5" SATA to USB adapter with power supply (€18)
51
* One Toshiba P300 4TB 3.5" SATA drive (€90). No RAID, but I copy important files to my own PC.
52
* Raspberry Pi 15W power adapter (€9)
53
* A 1.5m Cat6 cable (€3)
54
* SanDisk Extreme 128GB microSD card (€15)
55
* Official case (€6). Don't judge me, it's just a piece of plastic, and it's cheap. Don't forget
56
Raspberry Pi is not Apple.
57
58
Total: €221. Now, this is not the cheapest setup. I use that Pi for other things as well. You can
59
certainly have a cheaper setup:
60
61
### Low-cost setup
62
63
* Raspberry Pi 4 with 1GB of RAM (€40). I'd insist on getting a big Pi, as the Zero doesn't
64
have wired networking, only one USB port, and too little RAM.
65
* Cheaper no-name 3.5" SATA to USB adapter with power supply (€10)
66
* One Toshiba P300 1TB 3.5" SATA drive (€50)
67
* Phone charger (€6, if not already owned)
68
* No cable, use Wi-Fi (I assume you have Wi-Fi)
69
* SanDisk Ultra 32GB microSD card (€7)
70
* No case
71
72
Total: €113, with drive included!
73
74
What about a "real" NAS?
75
76
Cheapest NAS I could find is €180! It is 1-bay and has 1GB of RAM. It doesn't do anything else,
77
it doesn't use standard protocols, it runs a proprietary OS, it's bulkier and much more expensive.
78
(The brand is Synology.)
79
80
No, it doesn't come with drives. Using the same drive as above, it would reach €230.
81
82
Well, it's plug-and-play but that makes it much more restricted. Also, it does have a warranty
83
and professional support. However, it's not the kind of NAS a business would use either.
84
85
Setup (skip if you already have the computer set up)
86
----------------------------------------------------
87
88
1. Flash your microSD card. Raspberry Pi recommends [their own (free) software](https://www.raspberrypi.org/software/),
89
but there are other methods as well. Choose an OS without a GUI: Raspberry Pi OS Lite, Ubuntu
90
Server or plain Debian. Make sure to enable SSH and preset the Wi-Fi settings if you're going
91
to use Wi-Fi.
92
2. Put the card in the computer, plug in the network and start it.
93
3. Find out the IP address of the computer. You can use your router's web interface, or a network
94
scanner. Now is a good time to set up a static IP address for the computer, forward its ports
95
and get dynamic DNS if you want to access it from the internet. [YDNS](https://ydns.io/) is a
96
really simple and free service for that. Their official client is also free software.
97
4. Connect to your server via SSH. On GNU/Linux or Macintosh you can use the preinstalled `ssh`
98
client. On Windows, you can use [PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html).
99
5. Set a root password with `sudo passwd`. Then `su`.
100
6. Update the system with `apt update && apt -y upgrade`.
101
102
Set up the drives
103
-----------------
104
105
You must edit the `/etc/fstab` to automatically mount the drives on boot. If you know how to do
106
this, you can do it yourself. For the rest of the tutorial the mount point will be `/storage`.
107
108
1. Turn off the computer and plug in the drive(s).
109
2. Turn on the computer.
110
3. `lsblk` to find the drive(s). They should be `/dev/sd` followed by a letter. For now we'll
111
only take car of one drive, but you can do the same for multiple drives.
112
4. Find the drive's UUID with `blkid /dev/sdX1`.
113
5. Add the drive to the `/etc/fstab` as such:
114
```
115
UUID=00000000-0000-0000-0000-000000000000 /storage ext4 defaults 0 2
116
```
117
118
Replace the UUID with the one you found and, if you want, the mount point (`/storage` in this
119
case) with another one, and the filesystem to match the one you formatted the drive with. Change
120
the last column to `0` to disable fsck for that drive.
121
6. `mkdir /storage` to create the mount point.
122
7. Reboot.
123
124
Give each user their private directory
125
--------------------------------------
126
127
We'll mirror the home layout. However, we won't move the existing home directories, because then
128
users can't use the SD card, and you may have some other apps to run on the server that you want
129
to be separate from the mass storage.
130
131
Write a systemd service to create the directories on boot. Give it a name inside `/etc/systemd/system/`,
132
like `/etc/systemd/system/setup_storage_dirs.service`
133
134
```ini
135
[Unit]
136
Description=Update private user storage spaces
137
138
[Service]
139
Type=oneshot
140
ExecStart=/usr/bin/python3 /usr/local/bin/setup_storage_dirs.py
141
142
[Install]
143
WantedBy=multi-user.target
144
```
145
146
And the script `/usr/local/bin/setup_storage_dirs.py`:
147
148
```python
149
#!/bin/python3
150
151
import os
152
import subprocess
153
from pathlib import Path
154
155
homes = Path("/home").glob("*")
156
storage_path = Path("/storage") # change this if you changed the mount point
157
158
for home in homes:
159
if home.is_dir():
160
user = home.name
161
storage = storage_path/user
162
bound = home/"Storage" # change this if you'd like a different name
163
if not storage.exists():
164
os.makedirs(storage)
165
os.chown(storage, home.stat().st_uid, home.stat().st_gid)
166
os.chmod(storage, 0o700)
167
if not bound.is_dir():
168
os.makedirs(bound)
169
os.chown(bound, home.stat().st_uid, home.stat().st_gid)
170
os.chmod(bound, 0o700)
171
172
# Using bind mounts; some file managers don't like symlinks
173
subprocess.run(["mount", "--bind", str(storage), str(bound)])
174
```
175
176
Reboot. Now each user has a private directory on your drive at `~/Storage`. You can add more
177
users; each will get their own directory only they can access.
178
179
Using the server
180
----------------
181
182
This guide does not cover setting up sharing protocols. However, we will use SFTP (FTP over SSH)
183
because it's plug-and-play, the speed difference is negligible, it integrates with system users,
184
it's native (works in the system file manager, no web needed) and, last but not least, it's encrypted.
185
186
Since you set up the OS to use SSH, you can use SFTP. All file managers can connect to SFTP
187
servers, except for Windows Explorer, in which case you can use WinSCP. This is not my problem,
188
it's Microsoft's problem. Even the preinstalled file manager on Samsung phones can use an SFTP
189
add-on. Not like Next"cloud"¹ is more integrated.
190
191
If you want to mount it there is the `sshfs` program as well. This is useful if you don't like
192
GUI file managers or want easier access to the files.
193
194
### SFTP in the terminal
195
196
```sh
197
sftp -oPort=22 user@server
198
```
199
200
If you use port 22, you can omit the oPort option. The commands are identical to the ones in the
201
classic `ftp` client.
202
203
### SSHFS
204
205
```sh
206
sshfs user@server:/storage/user ~/server
207
```
208
209
Second argument is the "mount point". If you want to unmount it:
210
211
```sh
212
fusermount -u ~/server
213
```
214
215
### SFTP in the file manager
216
217
Graphically, you can access SFTP by typing `sftp://user@server` in the address bar (works in most
218
cases) or by finding an option to connect to a server.
219
220
#### Nemo (GNU/Linux)
221
File > Connect to server > select type `SSH`
222
223
#### Material Files (Android)
224
Menu > Add storage > SFTP server
225
226
#### Amaze (Android)
227
Plus button > Cloud connection > SCP/SFTP Connection
228
229
#### Windows
230
Windows Explorer doesn't natively support SFTP. WinSCP is a good client for it, and it's free
231
software.
232
233
#### Samsung phone file manager (Android)
234
Storage space section > Network storage space > Update the app when prompted > Plus > SFTP server
235